// KeyboardBacklightTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h> 
#include <iostream>
#include <Pm.h>
#include <gtest/gtest.h>
#include <WinIoCtl.h>
#include <gpio.h>

using namespace std;

#define D10K_GPIO_KEYPAD_BL_EN	 86

#define AUX_DRIVER_NAME L"AUX1:"
#define IOCTL_AUXPROC_SET_BACKLIGHT CTL_CODE(FILE_DEVICE_HAL, 2812, METHOD_BUFFERED, FILE_ANY_ACCESS) 

extern "C" BOOL WINAPI NLedGetDeviceInfo( UINT	nInfoId, void	*pOutput );
extern "C" BOOL WINAPI NLedSetDevice( UINT	nDeviceId, void	*pInput );


class KbdBacklight
{
private:
	UINT16	KBD_BLT_ON;
	UINT16 KBD_BLT_OFF;
	HANDLE hAuxDrv;

	bool KbdBacklightOn(void)
	{ 
		//D7800 || DBlack
		HANDLE         m_hGPIO=NULL;
		DWORD          m_dwKeypadBacklightEnable=D10K_GPIO_KEYPAD_BL_EN;
		m_hGPIO = GPIOOpen();
		GPIOSetMode(m_hGPIO, m_dwKeypadBacklightEnable, GPIO_DIR_OUTPUT );
		GPIOSetBit(m_hGPIO, m_dwKeypadBacklightEnable);
		GPIOClose(m_hGPIO);
		//D99EX
		USHORT keypadBackLightOn = KBD_BLT_ON;
		if (!DeviceIoControl(hAuxDrv, IOCTL_AUXPROC_SET_BACKLIGHT, &keypadBackLightOn, sizeof(USHORT), NULL, 0, NULL, NULL))
		{
			return 0;
		}
		return 1;
	}
	bool KbdBacklightOff(void)
	{ 
		//D7800 || DBlack
		HANDLE         m_hGPIO=NULL;
		DWORD 	 m_dwKeypadBacklightEnable=D10K_GPIO_KEYPAD_BL_EN;
		m_hGPIO = GPIOOpen();
		GPIOSetMode(m_hGPIO, m_dwKeypadBacklightEnable, GPIO_DIR_OUTPUT );
		GPIOClrBit(m_hGPIO, m_dwKeypadBacklightEnable);
		GPIOClose(m_hGPIO);
		//D99EX
		USHORT keypadBackLightOff = KBD_BLT_OFF;
		if (!DeviceIoControl(hAuxDrv, IOCTL_AUXPROC_SET_BACKLIGHT, &keypadBackLightOff, sizeof(USHORT), NULL, 0, NULL, NULL))
		{
			return 0;
		}
		return 1;    
	}

public:
	int initial()
	{
		KBD_BLT_ON = 0x1;
		KBD_BLT_OFF = 0x0;
		cout<<"reset the keyboard backlight"<<endl;
		
		KbdBacklightOff();

		cout<<"done!";
		cout<<endl<<"------------------------------------------------------"<<endl;
		return 1;
	}
	

	int kbdLed()
	{
		hAuxDrv = CreateFile(AUX_DRIVER_NAME,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
		if (hAuxDrv== INVALID_HANDLE_VALUE)
		{
			hAuxDrv = NULL;
			return 0;
		}
		for(int i=0;i<10;++i)
		{
//     		cout<<"back light on"<<endl;
			if(!KbdBacklightOn())
				return 0;
			Sleep(1*1000);
//   		cout<<"back light off"<<endl;
			if(!KbdBacklightOff())
				return 0;
			Sleep(1*1000);
		}
		cout<<endl<<"------------------------------------------------------"<<endl;
		return 1;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}

class KeyboardBacklightTest : public testing::Test
{
protected:
	static void SetUpTestCase()
	{
		kbd = new KbdBacklight();
		cout<<"start"<<endl;
	}
	static void TearDownTestCase()
	{
		delete kbd;
	}
	static KbdBacklight* kbd;
};
KbdBacklight* KeyboardBacklightTest::kbd = NULL;


TEST_F(KeyboardBacklightTest,INITIAL)
{
	EXPECT_EQ(1,kbd->initial());
}

TEST_F(KeyboardBacklightTest,BackLight)
{
	EXPECT_EQ(1,kbd->kbdLed());
	Sleep(3000);
}